<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js">var Microsoft;
(function (Microsoft) {
    (function (ProjectOxford) {
        (function (SpeechRecognition) {
            (function (SpeechRecognitionMode) {
                SpeechRecognitionMode._map = [];
                SpeechRecognitionMode._map[0] = &quot;shortPhrase&quot;;
                SpeechRecognitionMode.shortPhrase = 0;
                SpeechRecognitionMode._map[1] = &quot;longDictation&quot;;
                SpeechRecognitionMode.longDictation = 1;
            })(SpeechRecognition.SpeechRecognitionMode || (SpeechRecognition.SpeechRecognitionMode = {}));
            var SpeechRecognitionMode = SpeechRecognition.SpeechRecognitionMode;
            var MicrophoneRecognitionClient = (function () {
                function MicrophoneRecognitionClient(prefs) {
                    this.onPartialResponseReceived = null;
                    this.onFinalResponseReceived = null;
                    this.onIntentReceived = null;
                    this.onError = null;
                    this._prefs = prefs;
                    this._sr = new Bing.Speech();
                }
                MicrophoneRecognitionClient.prototype.startMicAndRecognition = function () {
                    var _this = this;
                    this._sr.onresult = function (e) {
                        if (e.results[e.resultIndex].final) {
                            _this.onFinalResponseReceived(e.results[e.resultIndex]);
                        } else {
                            _this.onPartialResponseReceived(e.results[e.resultIndex][0].transcript);
                        }
                    };
                    this._sr.onerror = function (e) {
                        if (_this.onError) {
                            _this.onError(-1, JSON.stringify(e));
                        }
                    };
                    Bing.Platform.getCU().done(function (cu) {
                        cu.preferences = _this._prefs;
                        cu.onintent = function (intent) {
                            if (_this.onIntentReceived) {
                                _this.onIntentReceived(intent.payload);
                            }
                        };
                        _this._sr.start();
                    });
                };
                MicrophoneRecognitionClient.prototype.endMicAndRecognition = function () {
                    this._sr.stop();
                };
                return MicrophoneRecognitionClient;
            })();
            SpeechRecognition.MicrophoneRecognitionClient = MicrophoneRecognitionClient;            
            var DataRecognitionClient = (function () {
                function DataRecognitionClient(prefs) {
                    this.onPartialResponseReceived = null;
                    this.onFinalResponseReceived = null;
                    this.onIntentReceived = null;
                    this.onError = null;
                    this._prefs = prefs;
                    this._start = true;
                    this._sr = new Bing.Speech();
                }
                DataRecognitionClient.prototype.sendAudio = function (buffer, actualAudioBytesInBuffer) {
                    var _this = this;
                    var src = new Bing.ArrayBufferSource();
                    src.setBuffer(buffer);
                    this._sr.mediaSource = src;
                    this._sr.onresult = function (e) {
                        if (e.results[e.resultIndex].final) {
                            _this.onFinalResponseReceived(e.results[e.resultIndex]);
                        } else {
                            _this.onPartialResponseReceived(e.results[e.resultIndex][0].transcript);
                        }
                    };
                    this._sr.onerror = function (e) {
                        if (_this.onError) {
                            _this.onError(-1, JSON.stringify(e));
                        }
                    };
                    Bing.Platform.getCU().done(function (cu) {
                        cu.preferences = _this._prefs;
                        cu.onintent = function (intent) {
                            if (_this.onIntentReceived) {
                                _this.onIntentReceived(intent.payload);
                            }
                        };
                        _this._sr.start();
                    });
                };
                DataRecognitionClient.prototype.endAudio = function () {
                    this._start = true;
                };
                return DataRecognitionClient;
            })();
            SpeechRecognition.DataRecognitionClient = DataRecognitionClient;            
            (function (SpeechRecognitionServiceFactory) {
                function createDataClient(speechRecognitionMode, language, primaryKey, secondaryKey) {
                    return new SpeechRecognition.DataRecognitionClient(createPrefs(speechRecognitionMode, language, primaryKey, secondaryKey));
                }
                SpeechRecognitionServiceFactory.createDataClient = createDataClient;
                function createDataClientWithIntent(language, primaryKey, secondaryKey, luisAppId, luisSubscriptionId) {
                    var prefs = createPrefs(SpeechRecognition.SpeechRecognitionMode.shortPhrase, language, primaryKey, secondaryKey);
                    prefs.luisAppId = luisAppId;
                    prefs.luisSubscriptionId = luisSubscriptionId;
                    return new SpeechRecognition.DataRecognitionClient(prefs);
                }
                SpeechRecognitionServiceFactory.createDataClientWithIntent = createDataClientWithIntent;
                function createMicrophoneClient(speechRecognitionMode, language, primaryKey, secondaryKey) {
                    return new SpeechRecognition.MicrophoneRecognitionClient(createPrefs(speechRecognitionMode, language, primaryKey, secondaryKey));
                }
                SpeechRecognitionServiceFactory.createMicrophoneClient = createMicrophoneClient;
                function createMicrophoneClientWithIntent(language, primaryKey, secondaryKey, luisAppId, luisSubscriptionId) {
                    var prefs = createPrefs(SpeechRecognition.SpeechRecognitionMode.shortPhrase, language, primaryKey, secondaryKey);
                    prefs.luisAppId = luisAppId;
                    prefs.luisSubscriptionId = luisSubscriptionId;
                    return new SpeechRecognition.MicrophoneRecognitionClient(prefs);
                }
                SpeechRecognitionServiceFactory.createMicrophoneClientWithIntent = createMicrophoneClientWithIntent;
                SpeechRecognitionServiceFactory.BaseSpeechUrl = &quot;https://websockets.platform.bing.com/ws/speech/recognize&quot;;
                function createPrefs(speechRecognitionMode, language, primaryKey, secondaryKey) {
                    var serviceUri = SpeechRecognitionServiceFactory.BaseSpeechUrl;
                    switch(speechRecognitionMode) {
                        case SpeechRecognition.SpeechRecognitionMode.longDictation:
                            serviceUri += &quot;/continuous&quot;;
                            break;
                    }
                    return {
                        serviceUri: serviceUri,
                        locale: language,
                        clientId: primaryKey,
                        clientSecret: secondaryKey,
                        clientVersion: &quot;4.0.150429&quot;,
                        authenticationScheme: &quot;MAIS&quot;
                    };
                }
                SpeechRecognitionServiceFactory.createPrefs = createPrefs;
            })(SpeechRecognition.SpeechRecognitionServiceFactory || (SpeechRecognition.SpeechRecognitionServiceFactory = {}));
            var SpeechRecognitionServiceFactory = SpeechRecognition.SpeechRecognitionServiceFactory;
        })(ProjectOxford.SpeechRecognition || (ProjectOxford.SpeechRecognition = {}));
        var SpeechRecognition = ProjectOxford.SpeechRecognition;
    })(Microsoft.ProjectOxford || (Microsoft.ProjectOxford = {}));
    var ProjectOxford = Microsoft.ProjectOxford;
})(Microsoft || (Microsoft = {}));
var Bing;
(function (Bing) {
    Bing._cu;
    Bing._cuDeferred = [];
    Bing._window = window;
    Bing._defaultVoiceName = &quot;Microsoft Server Speech Text to Speech Voice (en-US, ZiraRUS)&quot;;
    function write(text) {
        if (Bing._window.onlog) {
            Bing._window.onlog(text);
        }
        if (console &amp;&amp; console.log) {
            console.log(text);
        }
    }
    Bing.write = write;
    function writeline(text) {
        write(text + &quot;\n&quot;);
    }
    Bing.writeline = writeline;
    function setValue(name, value) {
        var json = JSON.stringify(value);
        window.localStorage.setItem(name, json);
    }
    Bing.setValue = setValue;
    function getValue(name) {
        var json = window.localStorage.getItem(name);
        var ret;
        if (json !== null &amp;&amp; json != 'undefined') {
            ret = JSON.parse(json);
        }
        return ret;
    }
    Bing.getValue = getValue;
    function dispatchEvent(name) {
        var event = document.createEvent('Event');
        event.initEvent(name, true, true);
        window.dispatchEvent(event);
    }
    Bing.dispatchEvent = dispatchEvent;
    function dispatchAudioStart() {
        dispatchEvent('audiostart');
    }
    Bing.dispatchAudioStart = dispatchAudioStart;
    function dispatchAudioStop() {
        dispatchEvent('audiostop');
    }
    Bing.dispatchAudioStop = dispatchAudioStop;
    function useHttp() {
        return getValue(&quot;useHttp&quot;);
    }
    Bing.useHttp = useHttp;
    function devMode() {
        return getValue(&quot;devMode&quot;);
    }
    Bing.devMode = devMode;
    (function (SynthState) {
        SynthState._map = [];
        SynthState._map[0] = &quot;None&quot;;
        SynthState.None = 0;
        SynthState._map[1] = &quot;Pending&quot;;
        SynthState.Pending = 1;
        SynthState._map[2] = &quot;Started&quot;;
        SynthState.Started = 2;
    })(Bing.SynthState || (Bing.SynthState = {}));
    var SynthState = Bing.SynthState;
    var Synthesis = (function () {
        function Synthesis() {
            var _this = this;
            this.voices = [];
            this._state = SynthState.None;
            this.paused = false;
            this.voices = null;
            var request = new XMLHttpRequest();
            request.open('GET', &quot;https://speech.platform.bing.com/synthesize/list/voices&quot;, true);
            request.responseType = 'json';
            request.onload = function () {
                if (request.readyState == 4) {
                    _this.voices = [];
                    if (request.status === 200) {
                        var list = handleJSONWebResponse(request);
                        for(var i = 0; i &lt; list.length; ++i) {
                            var vox = {
                                voiceURI: &quot;https://speech.platform.bing.com/synthesize&quot;,
                                name: list[i].Name,
                                lang: list[i].Locale,
                                localService: false,
                                default: list[i].Name == Bing._defaultVoiceName
                            };
                            _this.voices.push(vox);
                            if (vox.default) {
                                _this.defaultVoice = vox;
                            }
                        }
                    }
                }
            };
            request.send();
        }
        Object.defineProperty(Synthesis.prototype, &quot;pending&quot;, {
            get: function () {
                return this._state == SynthState.Pending;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(Synthesis.prototype, &quot;speaking&quot;, {
            get: function () {
                return this._state == SynthState.Started;
            },
            enumerable: true,
            configurable: true
        });
        Synthesis.prototype.speak = function (utterance) {
            var _this = this;
            var start = Date.now();
            this._state = SynthState.Pending;
            Platform.getCU().done(function (cu) {
                cu.onttsstart = function () {
                    _this._state = SynthState.Started;
                    if (utterance.onstart) {
                        utterance.onstart(_this.createEvent(&quot;start&quot;, utterance));
                    }
                };
                cu.onttsend = function () {
                    _this._state = SynthState.None;
                    if (utterance.onend) {
                        var e = _this.createEvent(&quot;end&quot;, utterance);
                        e.elapsedTime = (Date.now() - start) / 1000;
                        utterance.onend(e);
                    }
                };
                cu.onevent = function () {
                    _this._state = SynthState.None;
                    utterance.onerror(_this.createEvent(&quot;error&quot;, utterance));
                };
                if (!_this.context) {
                    if (!AudioContext) {
                        throw &quot;Sorry, your browser doesn't support WebAudio&quot;;
                    }
                    _this.context = new AudioContext();
                }
                cu.context = _this.context;
                cu.tts(utterance.text);
            });
        };
        Synthesis.prototype.cancel = function () {
            this._state = SynthState.None;
            Platform.getCU().done(function (cu) {
                cu.ttsStop();
            });
        };
        Synthesis.prototype.pause = function () {
        };
        Synthesis.prototype.resume = function () {
        };
        Synthesis.prototype.getVoices = function () {
            return this.voices;
        };
        Synthesis.prototype.createEvent = function (type, target) {
            var e = document.createEvent(&quot;UIEvent&quot;);
            e.initUIEvent(type, false, false, null, null);
            e.target = e.currentTarget = target;
            e.srcElement = target;
            e.timeStamp = Date.now();
            return e;
        };
        return Synthesis;
    })();
    Bing.Synthesis = Synthesis;    
    var msSpeechSynthesisUtterance = (function () {
        function msSpeechSynthesisUtterance(text) {
            this.text = text;
        }
        msSpeechSynthesisUtterance.prototype.removeEventListener = function (type, listener, useCapture) {
        };
        msSpeechSynthesisUtterance.prototype.addEventListener = function (type, listener, useCapture) {
        };
        msSpeechSynthesisUtterance.prototype.dispatchEvent = function (evt) {
            return false;
        };
        return msSpeechSynthesisUtterance;
    })();    
    var Speech = (function () {
        function Speech() {
            this._firstAudio = true;
            this.playbackAudioFilesOverride = false;
            this.grammars = null;
            this.lang = &quot;en-US&quot;;
            this.continuous = false;
            this.interimResults = true;
            this.maxAlternatives = -1;
            this.serviceURI = null;
        }
        Speech.prototype.removeEventListener = function (type, listener, useCapture) {
        };
        Speech.prototype.addEventListener = function (type, listener, useCapture) {
        };
        Speech.prototype.dispatchEvent = function (evt) {
            return false;
        };
        Speech.prototype.start = function () {
            var _this = this;
            if (this.onstart) {
                this.onstart(this.createEvent(&quot;start&quot;));
            }
            if (this.isMicSource) {
                if (!(navigator).getUserMedia) {
                    throw &quot;Sorry, your browser doesn't have microphone support.&quot;;
                }
                (navigator).getUserMedia({
                    audio: true
                }, function (stream) {
                    if (!_this.context) {
                        if (!AudioContext) {
                            throw &quot;Sorry, your browser doesn't support WebAudio&quot;;
                        }
                        _this.context = new AudioContext();
                    }
                    _this.currentSource = _this.context.createMediaStreamSource(stream);
                }, function () {
                    _this.HandleError();
                });
            } else {
                this.currentSource = this.mediaSource;
            }
        };
        Speech.prototype.stop = function () {
            if (this._currentSource) {
                Platform.getCU().done(function (cu) {
                    cu.disconnect();
                });
                if (this._currentSource) {
                    this._currentSource.disconnect();
                }
                this._currentSource = null;
            }
            if (this._currentDestination) {
                this._currentDestination.disconnect();
                this._currentDestination = null;
            }
        };
        Speech.prototype.abort = function () {
            this.stop();
        };
        Speech.prototype.HandleError = function () {
        };
        Object.defineProperty(Speech.prototype, &quot;currentSource&quot;, {
            set: function (source) {
                var _this = this;
                this._firstAudio = true;
                this._currentSource = source;
                Platform.getCU().done(function (cu) {
                    cu.onend = function () {
                        if (_this.onend) {
                            _this.onend(_this.createEvent(&quot;end&quot;));
                        }
                    };
                    cu.onaudioend = function () {
                        if (_this.onaudioend) {
                            _this.onaudioend(_this.createEvent(&quot;audioend&quot;));
                        }
                    };
                    cu.ondisplaytext = function (text) {
                        if (_this.onresult) {
                            var e = _this.createEvent(&quot;result&quot;);
                            e.resultIndex = 0;
                            e.results = {
                                length: 1,
                                0: {
                                    final: false,
                                    length: 1,
                                    0: {
                                        transcript: text
                                    }
                                }
                            };
                            _this.onresult(e);
                        }
                    };
                    cu.onresult = function (result) {
                        if (result.status == 301) {
                            if (_this.onnomatch) {
                                _this.onnomatch(_this.createEvent(&quot;nomatch&quot;));
                            }
                        } else if (_this.onresult) {
                            _this.onresult(result);
                        } else {
                            console.warn(&quot;Speech.onresult not set&quot;);
                        }
                    };
                    cu.onevent = function (statusCode) {
                        if (_this.onerror) {
                            var e = _this.createEvent(&quot;error&quot;);
                            e.error = &quot;received a speech error &quot; + statusCode;
                            _this.onerror(e);
                        }
                        if (_this.onend) {
                            _this.onend(_this.createEvent(&quot;end&quot;));
                        }
                    };
                    _this._currentDestination = _this.createRecogitionDestination(_this._currentSource, cu, null);
                });
            },
            enumerable: true,
            configurable: true
        });
        Speech.prototype.createEvent = function (type) {
            var e = document.createEvent(&quot;UIEvent&quot;);
            e.initUIEvent(type, false, false, null, null);
            e.target = e.currentTarget = this;
            e.srcElement = this;
            e.timeStamp = Date.now();
            return e;
        };
        Speech.prototype.createRecogitionDestination = function (source, cu, onaudioprocess) {
            var _this = this;
            var destination = source.context.createScriptProcessor(4096, 1, 1);
            destination.onaudioprocess = function (e) {
                var inputBuffer = e.inputBuffer;
                if (_this._firstAudio) {
                    _this._firstAudio = false;
                    if (_this.onaudiostart) {
                        _this.onaudiostart(_this.createEvent(&quot;audiostart&quot;));
                    }
                }
                if (Bing._window.useStringArrays === true) {
                    cu.audioprocess(new StringAudioBuffer(inputBuffer));
                } else {
                    cu.audioprocess(inputBuffer);
                }
                if (onaudioprocess) {
                    onaudioprocess(e);
                }
                if (_this.playbackAudioFiles() &amp;&amp; !_this.isMicSource) {
                    var outputBuffer = e.outputBuffer;
                    for(var channel = 0; channel &lt; outputBuffer.numberOfChannels; channel++) {
                        var outputData = outputBuffer.getChannelData(channel);
                        var inputData = inputBuffer.getChannelData(channel);
                        for(var sample = 0; sample &lt; e.inputBuffer.length; sample++) {
                            outputData[sample] = inputData[sample];
                        }
                    }
                }
                if (Bing._window.msSpeechButton &amp;&amp; !Bing._window.isActiveX) {
                    Bing._window.msSpeechButton.audioprocess(e);
                }
            };
            source.connect(destination);
            cu.connect(source);
            destination.connect(source.context.destination);
            return destination;
        };
        Object.defineProperty(Speech.prototype, &quot;isMicSource&quot;, {
            get: function () {
                return null === this.mediaSource || typeof this.mediaSource == 'undefined';
            },
            enumerable: true,
            configurable: true
        });
        Speech.prototype.playbackAudioFiles = function () {
            return getValue(&quot;playbackAudioFiles&quot;) || this.playbackAudioFilesOverride;
        };
        return Speech;
    })();
    Bing.Speech = Speech;    
    ;
    var Task = (function () {
        function Task() {
            this.completed = false;
            this.startTime = Date.now();
        }
        Task.prototype.complete = function () {
            this.signalComplete(true);
        };
        Task.prototype.resolve = function (result) {
            this.signalComplete(result);
        };
        Task.prototype.done = function (cb) {
            if (this.completed) {
                cb(this.result);
                return;
            }
            this.cb = cb;
        };
        Object.defineProperty(Task.prototype, &quot;elapsedTime&quot;, {
            get: function () {
                return (Date.now() - this.startTime) / 1000;
            },
            enumerable: true,
            configurable: true
        });
        Task.prototype.signalComplete = function (result) {
            this.result = result;
            this.completed = true;
            if (this.cb) {
                this.cb(this.result);
            }
        };
        return Task;
    })();
    Bing.Task = Task;    
    var StringAudioBuffer = (function () {
        function StringAudioBuffer(buffer) {
            this._audioBuffer = buffer;
            this.sampleRate = buffer.sampleRate;
            this.length = buffer.length;
            this.duration = buffer.duration;
            this.numberOfChannels = buffer.numberOfChannels;
        }
        StringAudioBuffer.prototype.getChannelData = function (channel) {
            var data = this._audioBuffer.getChannelData(channel);
            var a = [];
            var ret;
            var i;
            var byteStr;
            for(i = 0; i &lt; data.length; ++i) {
                byteStr = Math.floor((data[i] + 1.0) * 0x7fff).toString(16).replace('-', '');
                while(byteStr.length &lt; 4) {
                    byteStr = &quot;0&quot; + byteStr;
                }
                a.push(byteStr);
            }
            ret = a.join(&quot;&quot;);
            return ret;
        };
        StringAudioBuffer.prototype.copyFromChannel = function (destination, channelNumber, startInChannel) {
            this._audioBuffer.copyFromChannel(destination, channelNumber, startInChannel);
        };
        StringAudioBuffer.prototype.copyToChannel = function (source, channelNumber, startInChannel) {
            this._audioBuffer.copyToChannel(source, channelNumber, startInChannel);
        };
        return StringAudioBuffer;
    })();
    Bing.StringAudioBuffer = StringAudioBuffer;    
    function CreateActiveXObject(name) {
        try  {
            return new ActiveXObject(name);
        } catch (e) {
            return null;
        }
    }
    Bing.CreateActiveXObject = CreateActiveXObject;
    (function (Platform) {
        function isEdge() {
            return navigator.userAgent.indexOf(&quot;Edge/&quot;) != -1;
        }
        Platform.isEdge = isEdge;
        function isSafari() {
            return navigator.userAgent.indexOf(&quot;AppleWebKit&quot;) != -1;
        }
        Platform.isSafari = isSafari;
        function supportsPPAPI() {
            if (Bing._window.chrome &amp;&amp; !isEdge() &amp;&amp; navigator.userAgent.indexOf(&quot;Chrome/&quot;)) {
                return true;
            } else {
                return false;
            }
        }
        Platform.supportsPPAPI = supportsPPAPI;
        function supportsNPAPI() {
            if (!isEdge() &amp;&amp; ((navigator.userAgent.indexOf(&quot;Firefox&quot;) != -1) || isSafari())) {
                return true;
            } else {
                return false;
            }
        }
        Platform.supportsNPAPI = supportsNPAPI;
        function supportsActiveX() {
            if ((navigator.userAgent.indexOf(&quot;Trident&quot;) != -1)) {
                return true;
            } else {
                return false;
            }
        }
        Platform.supportsActiveX = supportsActiveX;
        function getCU() {
            var task = new Bing.Task();
            if (Bing._cu) {
                task.resolve(Bing._cu);
            } else {
                Bing._cuDeferred.push(task);
            }
            return task;
        }
        Platform.getCU = getCU;
    })(Bing.Platform || (Bing.Platform = {}));
    var Platform = Bing.Platform;
    function initialize() {
        var i;
        Bing._window.useStringArrays = false;
        Bing._cu = createSpeech();
        if (!Bing._cuDeferred) {
            Bing._cuDeferred = [];
        }
        if (Bing._cu) {
            for(i = 0; i &lt; Bing._cuDeferred.length; ++i) {
                Bing._cuDeferred[i].resolve(Bing._cu);
            }
        }
    }
    Bing.initialize = initialize;
    function decodeAudioData(context, audioData, successCallback, errorCallback) {
        if (Bing._window.isActiveX) {
            audioData = new Int8Array(audioData);
        }
        context.decodeAudioData(audioData, successCallback, errorCallback);
    }
    Bing.decodeAudioData = decodeAudioData;
    function handleJSONWebResponse(xhr) {
        if (typeof (xhr.response) == &quot;string&quot;) {
            return JSON.parse(xhr.response);
        }
        return xhr.response;
    }
    Bing.handleJSONWebResponse = handleJSONWebResponse;
    var OxfordAuthenticator = (function () {
        function OxfordAuthenticator() {
        }
        OxfordAuthenticator.prototype.authenticate = function (primaryKey, secondaryKey) {
            var _this = this;
            var task = new Task();
            var now = Date.now();
            var params = [
                &quot;grant_type=client_credentials&amp;client_id=&quot;, 
                encodeURIComponent(primaryKey), 
                &quot;&amp;client_secret=&quot;, 
                encodeURIComponent(secondaryKey), 
                &quot;&amp;scope=&quot;, 
                encodeURIComponent(&quot;https://speech.platform.bing.com&quot;)
            ].join(&quot;&quot;);
            if (!this._response || !this._expireTime || Date.now() &gt;= this._expireTime.getTime()) {
                writeline(&quot;refreshing token&quot;);
                var xhr = new XMLHttpRequest();
                xhr.open('POST', &quot;https://oxford-speech.cloudapp.net/token/issueToken&quot;, true);
                xhr.responseType = 'json';
                xhr.onload = function () {
                    if (xhr.readyState == 4) {
                        if (xhr.status === 200) {
                            _this._response = handleJSONWebResponse(xhr);
                            _this._expireTime = new Date(Date.now() + parseInt(_this._response.expires_in) * 1000);
                            task.resolve(&quot;Bearer &quot; + _this._response.access_token);
                        } else {
                            task.resolve(null);
                        }
                    }
                };
                xhr.onerror = function () {
                    task.resolve(null);
                };
                xhr.setRequestHeader(&quot;Content-type&quot;, &quot;application/x-www-form-urlencoded&quot;);
                xhr.send(params);
            } else {
                task.resolve(&quot;Bearer &quot; + this._response.access_token);
            }
            return task;
        };
        return OxfordAuthenticator;
    })();    
    var AdmAuthenticator = (function () {
        function AdmAuthenticator() {
        }
        AdmAuthenticator.prototype.authenticate = function (primaryKey, secondaryKey) {
            var task = new Task();
            writeline(&quot;authenticate: &quot; + primaryKey + &quot; &quot; + secondaryKey);
            var params = &quot;grant_type=client_credentials&amp;client_id=&quot; + encodeURIComponent(primaryKey) + &quot;&amp;client_secret=&quot; + encodeURIComponent(secondaryKey) + &quot;&amp;scope=&quot; + encodeURIComponent(&quot;https://speech.platform.bing.com&quot;);
            var xhr = new XMLHttpRequest();
            xhr.open('POST', &quot;https://datamarket.accesscontrol.windows.net/v2/OAuth2-13&quot;, true);
            xhr.onload = function () {
                if (xhr.readyState == 4) {
                    if (xhr.status === 200) {
                        task.resolve(handleJSONWebResponse(xhr));
                    } else {
                        task.resolve(null);
                    }
                }
            };
            xhr.onerror = function () {
                task.resolve(null);
            };
            xhr.setRequestHeader(&quot;Content-type&quot;, &quot;application/x-www-form-urlencoded&quot;);
            xhr.send(params);
            return task;
        };
        return AdmAuthenticator;
    })();    
    var Riff = (function () {
        function Riff(sampleRate, bitsPerSample) {
            this._buffer = [];
            this._bitsPerSample = 8;
            this._channels = 1;
            this._sampleRate = 44100;
            this._bitsPerSample = bitsPerSample;
            this._sampleRate = sampleRate;
            this.appendString(&quot;RIFF&quot;);
            this.appendUINT32(0);
            this.appendString(&quot;WAVEfmt &quot;);
            this.appendUINT32(2 + 2 + 4 + 4 + 2 + 2);
            this.appendUINT16(Riff.WAVE_FORMAT_PCM);
            this.appendUINT16(this._channels);
            this.appendUINT32(this._sampleRate);
            this.appendUINT32(this._sampleRate * (this._bitsPerSample &gt;&gt; 3) * this._channels);
            this.appendUINT16(this._bitsPerSample &gt;&gt; 3);
            this.appendUINT16(this._bitsPerSample);
            this.appendString(&quot;data&quot;);
            this.appendUINT32(0);
        }
        Riff.WAVE_FORMAT_PCM = 1;
        Riff.WAVE_FORMAT_IEEE_FLOAT = 3;
        Riff.prototype.appendString = function (s) {
            for(var i = 0; i &lt; s.length; ++i) {
                this._buffer.push(s.charCodeAt(i));
            }
        };
        Riff.prototype.appendUINT32 = function (n) {
            this.appendUINT16(n);
            this.appendUINT16(n &gt;&gt; 16);
        };
        Riff.prototype.appendUINT16 = function (n) {
            this._buffer.push((n &amp; 0x00ff) &gt;&gt; 0);
            this._buffer.push((n &amp; 0xff00) &gt;&gt; 8);
        };
        Riff.prototype.toByteArray = function () {
            return this._buffer;
        };
        return Riff;
    })();    
    var LuisClient = (function () {
        function LuisClient(prefs) {
            this._prefs = prefs;
            switch(prefs.authenticationScheme) {
                case &quot;MAIS&quot;:
                    this._auth = new OxfordAuthenticator();
                    break;
                case &quot;ADM&quot;:
                    this._auth = new AdmAuthenticator();
                    break;
            }
        }
        LuisClient.kServiceUrl = &quot;https://api.projectoxford.ai/luis/v1/application?subscription-key=&quot;;
        LuisClient.prototype.getIntent = function (text) {
            var _this = this;
            var task = new Task();
            var request = new XMLHttpRequest();
            this._auth.authenticate(this._prefs.clientId, this._prefs.clientSecret).done(function (token) {
                if (!token) {
                    task.resolve(null);
                    return;
                }
                request.open('GET', [
                    LuisClient.kServiceUrl, 
                    _this._prefs.luisSubscriptionId, 
                    &quot;&amp;id=&quot;, 
                    _this._prefs.luisAppId, 
                    &quot;&amp;q=&quot;, 
                    text
                ].join(&quot;&quot;), true);
                request.setRequestHeader(&quot;Authorization&quot;, token);
                request.onload = function () {
                    if (request.readyState == 4 &amp;&amp; request.status === 200) {
                        var response = handleJSONWebResponse(request);
                        task.resolve(request.response);
                    } else {
                        task.resolve(null);
                    }
                };
                request.send();
            });
            return task;
        };
        return LuisClient;
    })();
    Bing.LuisClient = LuisClient;    
    var HTTPResultStatus = (function () {
        function HTTPResultStatus() { }
        HTTPResultStatus.SUCCESS = &quot;success&quot;;
        HTTPResultStatus.ERROR = &quot;error&quot;;
        return HTTPResultStatus;
    })();    
    var HttpClient = (function () {
        function HttpClient() {
            this.queue = [];
            this.responseFormat = &quot;json&quot;;
            writeline(&quot;Defaulting to http client&quot;);
            this.requestUri = &quot;?scenarios=smd&quot;;
            this.requestUri += &quot;&amp;appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5&quot;;
            this.requestUri += &quot;&amp;device.os=wp7&quot;;
            this.requestUri += &quot;&amp;version=3.0&quot;;
            this.requestUri += &quot;&amp;instanceid=565D69FF-E928-4B7E-87DA-9A750B96D9E3&quot;;
            this.requestUri += &quot;&amp;requestid=&quot; + &quot;2065F912-3699-408C-A80A-9D17F42B9692&quot;;
        }
        HttpClient.prototype.removeEventListener = function (type, listener, useCapture) {
        };
        HttpClient.prototype.addEventListener = function (type, listener, useCapture) {
            writeline(&quot;addEventListener: &quot; + type + &quot; &quot; + this);
        };
        HttpClient.prototype.dispatchEvent = function (evt) {
            return false;
        };
        HttpClient.prototype.connect = function (destination, output, input) {
            Bing._window.useStringArrays = false;
            this.sourceSampleRate = destination.context.sampleRate;
            this.sampleRate = 16000;
            if (this.sourceSampleRate &lt;= 0) {
                return;
            }
            this.connected = true;
            if (this.preferences.luisAppId &amp;&amp; this.preferences.luisSubscriptionId) {
                this.luis = new LuisClient(this.preferences);
            }
            this.queue = [];
            this.buffer = new Int8Array(new Riff(this.sampleRate, 8).toByteArray());
            this.offset = this.buffer.length;
        };
        HttpClient.prototype.disconnect = function () {
            if (this.connected) {
                this.connected = false;
                if (this.onaudioend) {
                    this.onaudioend();
                }
                this.send();
            }
        };
        Object.defineProperty(HttpClient.prototype, &quot;preferences&quot;, {
            get: function () {
                return this._preferences;
            },
            set: function (prefs) {
                this._preferences = prefs;
                this.auth = null;
            },
            enumerable: true,
            configurable: true
        });
        HttpClient.prototype.sendText = function (inputText) {
        };
        HttpClient.prototype.audioprocess = function (buffer) {
            this.appendAsUInt8(buffer.getChannelData(0));
        };
        HttpClient.prototype.tts = function (text, contentType, outputFormat) {
            var _this = this;
            if (!contentType) {
                contentType = &quot;text/plain&quot;;
            }
            if (!outputFormat) {
                outputFormat = &quot;riff-16khz-16bit-mono-pcm&quot;;
            }
            if (contentType === &quot;text/plain&quot;) {
                text = &quot;&lt;?xml version='1.0' encoding='UTF-8'?&gt;&quot; + &quot;&lt;speak version='1.0' xml:lang='&quot; + this.preferences.locale + &quot;'&gt;&quot; + &quot;&lt;voice xml:lang='&quot; + this.preferences.locale + &quot;' name='&quot; + Bing._defaultVoiceName + &quot;'&gt;&quot; + text + &quot;&lt;/voice&gt;&lt;/speak&gt;&quot;;
                contentType = &quot;application/ssml+xml&quot;;
            }
            var request = new XMLHttpRequest();
            request.open('POST', &quot;https://speech.platform.bing.com/synthesize&quot;, true);
            request.responseType = 'arraybuffer';
            request.setRequestHeader(&quot;X-MICROSOFT-OutputFormat&quot;, outputFormat);
            request.setRequestHeader(&quot;Content-Type&quot;, contentType);
            request.onload = function () {
                if (request.readyState == 4 &amp;&amp; request.status !== 200) {
                    _this.onevent(request.status);
                } else {
                    _this.renderAudio(_this.context, request.response);
                }
            };
            this.getToken().done(function (token) {
                if (!token) {
                    _this.dispatchError(-1);
                    return;
                }
                request.setRequestHeader(&quot;Authorization&quot;, token);
                if (_this.onttsstart) {
                    _this.onttsstart();
                }
                request.send(text);
            });
        };
        HttpClient.prototype.ttsStop = function () {
            var src = this.ttsSource;
            if (src &amp;&amp; this.context.state != &quot;suspended&quot;) {
                try  {
                    src.stop();
                } catch (e) {
                    writeline(&quot;ttsStop: buffer source failed to stop. state: &quot; + this.context.state + &quot; exception:&quot; + e);
                }
            }
        };
        HttpClient.prototype.getToken = function () {
            if (!this.auth) {
                switch(this.preferences.authenticationScheme) {
                    case &quot;MAIS&quot;:
                        this.auth = new OxfordAuthenticator();
                        break;
                    case &quot;ADM&quot;:
                        this.auth = new AdmAuthenticator();
                        break;
                }
            }
            return this.auth.authenticate(this.preferences.clientId, this.preferences.clientSecret);
        };
        HttpClient.prototype.send = function () {
            var _this = this;
            var result;
            this.getToken().done(function (token) {
                if (!token) {
                    _this.dispatchError(-1);
                    return;
                }
                var serviceUrl = _this.preferences.serviceUri.replace(&quot;/ws/speech&quot;, &quot;&quot;).replace(&quot;websockets.&quot;, &quot;speech.&quot;);
                writeline(&quot;connect: url &quot; + serviceUrl);
                var request = new XMLHttpRequest();
                request.open('POST', [
                    serviceUrl, 
                    _this.requestUri, 
                    &quot;&amp;locale=&quot;, 
                    _this.preferences.locale, 
                    &quot;&amp;format=&quot;, 
                    _this.responseFormat
                ].join(&quot;&quot;), true);
                request.responseType = _this.responseFormat;
                request.setRequestHeader(&quot;Content-Type&quot;, 'audio/wav; codec=&quot;audio/pcm&quot;; samplerate=' + _this.sampleRate);
                request.setRequestHeader(&quot;Authorization&quot;, token);
                token = token;
                request.onload = function () {
                    if (request.readyState == 4 &amp;&amp; request.status !== 200) {
                        _this.dispatchError(request.status);
                    } else {
                        result = handleJSONWebResponse(request);
                        if (result.header.status === HTTPResultStatus.ERROR) {
                            _this.dispatchError(-1);
                        } else {
                            _this.dispatchResult(result);
                        }
                    }
                };
                if (_this.buffer &amp;&amp; _this.buffer.length) {
                    request.send(_this.buffer);
                }
            });
        };
        HttpClient.prototype.dispatchError = function (statusCode) {
            if (this.onevent) {
                this.onevent(statusCode);
            }
        };
        HttpClient.prototype.dispatchResult = function (result) {
            var _this = this;
            var reco;
            if (result.results &amp;&amp; result.results.length &gt; 0 &amp;&amp; result.results[0].name) {
                reco = result.results[0].name;
            }
            if (this.luis &amp;&amp; this.onintent) {
                this.luis.getIntent(reco).done(function (r) {
                    _this.onintent({
                        payload: r
                    });
                });
            }
            if (this.onresult) {
                var phrases = [];
                for(var i = 0; i &lt; result.results.length; ++i) {
                    var r = result.results[i];
                    phrases.push({
                        lexical: r.lexical,
                        display: r.name,
                        inverseNormalization: null,
                        maskedInverseNormalization: null,
                        transcript: r.name,
                        confidence: parseFloat(r.confidence)
                    });
                }
                phrases[&quot;final&quot;] = true;
                var finalResult = {
                    resultIndex: 0,
                    results: {
                        length: 1,
                        0: phrases
                    },
                    interpretation: reco,
                    emma: null,
                    status: 200
                };
                this.onresult(finalResult);
            }
            if (this.onend) {
                this.onend();
            }
        };
        HttpClient.prototype.appendAsUInt8 = function (a) {
            var newBuffer;
            newBuffer = new Int8Array(a.length + this.offset);
            if (this.buffer) {
                newBuffer.set(this.buffer, 0);
            }
            this.buffer = newBuffer;
            var incrementBy = this.sourceSampleRate / this.sampleRate;
            for(var i = 0; i &lt; a.length; i += incrementBy) {
                this.buffer[this.offset++] = Math.floor((a[Math.floor(i)] - .5) * 128);
            }
        };
        HttpClient.prototype.renderAudio = function (context, audioData) {
            var _this = this;
            decodeAudioData(context, audioData, function (buffer) {
                writeline(&quot;completed decoding audio&quot;);
                _this.ttsSource = context.createBufferSource();
                _this.ttsSource.buffer = buffer;
                _this.ttsSource.connect(_this.context.destination);
                if (_this.onttsstart) {
                    _this.onttsstart();
                }
                _this.ttsSource.start(0);
                _this.ttsSource.onended = function () {
                    if (_this.onttsend) {
                        _this.onttsend();
                    }
                    _this.ttsSource = null;
                };
            }, function () {
                writeline(&quot;error decoding audio&quot;);
                _this.onevent(-1);
            });
        };
        return HttpClient;
    })();
    Bing.HttpClient = HttpClient;    
    var NaclClient = (function () {
        function NaclClient() {
            var _this = this;
            var chrome = Bing._window.chrome;
            var naclContainer;
            if (devMode() === true) {
                naclContainer = document.createElement(&quot;div&quot;);
                naclContainer.setAttribute(&quot;style&quot;, &quot;width:0;height:0&quot;);
                naclContainer.addEventListener('load', function (arg) {
                    var i;
                    writeline(&quot;plugin load:&quot; + arg);
                    for(i = 0; i &lt; Bing._cuDeferred.length; ++i) {
                        Bing._cuDeferred[i].resolve(Bing._cu);
                    }
                }, true);
                naclContainer.addEventListener('error', function (arg) {
                    writeline(&quot;plugin error:&quot; + arg);
                }, true);
                naclContainer.addEventListener('crash', function (arg) {
                    writeline(&quot;plugin crash:&quot; + arg);
                }, true);
                naclContainer.addEventListener('message', function (arg) {
                    _this.handleMessage(arg);
                }, true);
                window.document.body.appendChild(naclContainer);
                var npPlugin = document.createElement(&quot;embed&quot;);
                npPlugin.setAttribute(&quot;type&quot;, &quot;application/x-pnacl&quot;);
                npPlugin.setAttribute(&quot;src&quot;, &quot;/bin/pepper_speech.nmf&quot;);
                npPlugin.setAttribute(&quot;id&quot;, &quot;pepper_speech&quot;);
                this._module = npPlugin;
                naclContainer.appendChild(npPlugin);
            } else {
                var port = chrome.runtime.connect(NaclClient.kKeyId);
                port.onMessage.addListener(function (arg) {
                    _this.handleMessage(arg);
                });
                port.onConnect = function (arg) {
                    writeline(&quot;port onConnect:&quot; + arg);
                };
                this._module = port;
            }
        }
        NaclClient.kKeyId = &quot;jffoigoenpgbgnhpchggjapfijhffghe&quot;;
        NaclClient.prototype.postMessage = function (arg) {
            if (Bing._window.naclNotInstalled === true) {
                return;
            }
            try  {
                this._module.postMessage(arg);
            } catch (e) {
                if (Bing._window.chrome.runtime.lastError) {
                    Bing._window.naclNotInstalled = true;
                    Bing.initialize();
                }
            }
        };
        NaclClient.prototype.handleMessage = function (arg) {
            var data;
            if ((arg).name) {
                data = arg;
            } else {
                data = arg.data;
            }
            if (this[data.name]) {
                this[data.name](data.data);
            }
        };
        NaclClient.prototype.log = function (arg) {
            var d = new Date();
            Bing.write(&quot;[&quot; + d.toISOString() + &quot;] &quot; + arg);
        };
        NaclClient.prototype.removeEventListener = function (type, listener, useCapture) {
        };
        NaclClient.prototype.addEventListener = function (type, listener, useCapture) {
            writeline(&quot;addEventListener: &quot; + type + &quot; &quot; + this);
        };
        NaclClient.prototype.dispatchEvent = function (evt) {
            return false;
        };
        NaclClient.prototype.connect = function (destination, output, input) {
            this.postMessage([
                &quot;connect&quot;, 
                destination.context.sampleRate
            ]);
        };
        NaclClient.prototype.disconnect = function () {
            this.postMessage([
                &quot;disconnect&quot;
            ]);
        };
        Object.defineProperty(NaclClient.prototype, &quot;preferences&quot;, {
            get: function () {
                return this._preferences;
            },
            set: function (prefs) {
                this._preferences = prefs;
                this.postMessage([
                    &quot;setPreferences&quot;, 
                    prefs
                ]);
            },
            enumerable: true,
            configurable: true
        });
        NaclClient.prototype.sendText = function (inputText) {
        };
        NaclClient.prototype.audioprocess = function (buffer) {
            this.postMessage([
                &quot;audioprocess&quot;, 
                buffer.getChannelData(0), 
                buffer
            ]);
        };
        NaclClient.prototype.tts = function (text, contentType, outputFormat) {
            this.postMessage([
                &quot;tts&quot;, 
                text, 
                contentType, 
                outputFormat
            ]);
            if (this.onttsstart) {
                this.onttsstart();
            }
        };
        NaclClient.prototype.ttsStop = function () {
            this.postMessage([
                &quot;ttsstop&quot;
            ]);
        };
        return NaclClient;
    })();
    Bing.NaclClient = NaclClient;    
    function shouldCreateHttp() {
        if (Platform.isEdge()) {
            return true;
        }
        if (((Bing)).checkedForPluginInstall &amp;&amp; ((Bing)).checkedForPluginInstall() !== true) {
            return false;
        }
        return true;
    }
    Bing.shouldCreateHttp = shouldCreateHttp;
    function createSpeech() {
        var cu;
        if (useHttp()) {
            return new HttpClient();
        } else if (Bing._window.naclNotInstalled !== true) {
            if (Platform.supportsPPAPI()) {
                Bing._window.chrome.runtime.sendMessage(NaclClient.kKeyId, &quot;__hello&quot;, null, function (response) {
                    Bing._window.naclNotInstalled = (response !== &quot;__hello&quot;);
                    if (Bing._window.naclNotInstalled) {
                        Bing._cu = new HttpClient();
                    } else {
                        Bing._cu = new NaclClient();
                    }
                    for(var i = 0; i &lt; Bing._cuDeferred.length; ++i) {
                        Bing._cuDeferred[i].resolve(Bing._cu);
                    }
                });
                return null;
            } else if (Platform.supportsNPAPI()) {
                var npPlugin = document.createElement(&quot;embed&quot;);
                var promise = new Task();
                (npPlugin).type = &quot;application/x-bingspeech&quot;;
                (npPlugin).data = &quot;data:application/x-bingspeech,&quot;;
                npPlugin.setAttribute(&quot;style&quot;, &quot;width:0;height:0&quot;);
                promise.done(function () {
                    window.document.body.appendChild(npPlugin);
                    try  {
                        (npPlugin)();
                        Bing._cu = npPlugin;
                        (Bing._cu).origin = window.window.location.href;
                        Bing._window.useStringArrays = true;
                    } catch (e) {
                        Bing._cu = new HttpClient();
                    }
                    for(var i = 0; i &lt; Bing._cuDeferred.length; ++i) {
                        Bing._cuDeferred[i].resolve(Bing._cu);
                    }
                });
                if (window.document.body) {
                    promise.complete();
                } else {
                    document.addEventListener('DOMContentLoaded', function () {
                        promise.complete();
                    });
                }
                return null;
            } else if (Platform.supportsActiveX()) {
                cu = CreateActiveXObject(&quot;Bing.Host&quot;);
                if (cu) {
                    (cu).origin = window.window.location.href;
                    Bing._window.isActiveX = true;
                }
            }
        }
        if (!cu &amp;&amp; shouldCreateHttp()) {
            writeline(&quot;Defaulting to http client&quot;);
            return new HttpClient();
        }
        if (!cu &amp;&amp; ((Bing)).SpeechInstaller) {
            var installer = new ((Bing)).SpeechInstaller();
            installer.show(&quot;Install the Bing Speech Extender&quot;);
            return installer;
        }
        return cu;
    }
    Bing.createSpeech = createSpeech;
    var WebAudioSource = (function () {
        function WebAudioSource(url, context) {
            this.numberOfInputs = 1;
            this.numberOfOutputs = 1;
            this.channelCount = 1;
            this._url = url;
            if (!context) {
                if (!AudioContext) {
                    throw &quot;Sorry, your browser doesn't support WebAudio&quot;;
                }
                this.context = new AudioContext();
            } else {
                this.context = context;
            }
            writeline(&quot;LogSendAudio: url=[&quot; + url + &quot;], contentType=[audio/basic]&quot;);
        }
        WebAudioSource.prototype.removeEventListener = function (type, listener, useCapture) {
        };
        WebAudioSource.prototype.addEventListener = function (type, listener, useCapture) {
        };
        WebAudioSource.prototype.dispatchEvent = function (evt) {
            return false;
        };
        WebAudioSource.prototype.connect = function (destination, output, input) {
            var _this = this;
            var request;
            this._destination = destination;
            if (null != this._aBuffer &amp;&amp; this._aBuffer.byteLength &gt; 0) {
                this.bufferReceived();
                return;
            }
            writeline(&quot;connect &quot; + this);
            request = new XMLHttpRequest();
            request.open('GET', this._url, true);
            request.responseType = 'arraybuffer';
            request.onload = function () {
                if (request.readyState == 4 &amp;&amp; request.status !== 200) {
                    _this.handleEnd(request.status);
                } else {
                    _this._aBuffer = request.response;
                    _this.bufferReceived();
                }
            };
            request.send();
        };
        WebAudioSource.prototype.disconnect = function () {
            var dest = this._destination;
            if (null == dest) {
                return;
            }
            if (this._bufferSource) {
                this._bufferSource.disconnect();
                this._bufferSource.stop();
            }
            this._started = false;
            dest.disconnect();
            this._destination = null;
            Platform.getCU().done(function (cu) {
                cu.disconnect();
            });
            if (this.onended) {
                this.onended();
            }
            dispatchAudioStop();
        };
        WebAudioSource.prototype.start = function (when, offset, duration) {
        };
        WebAudioSource.prototype.stop = function (when) {
        };
        WebAudioSource.prototype.setBuffer = function (buffer) {
            this._aBuffer = buffer;
            this.bufferReceived();
        };
        WebAudioSource.prototype.bufferReceived = function () {
            var _this = this;
            var bufferSource;
            bufferSource = this.context.createBufferSource();
            decodeAudioData(this.context, this._aBuffer, function (buffer) {
                _this._bufferSource = bufferSource;
                _this._bufferSource.buffer = buffer;
                _this.onBufferLoaded();
            }, function () {
                writeline(&quot;error decoding WebAudio&quot;);
                _this.handleEnd();
            });
        };
        WebAudioSource.prototype.handleEnd = function (err) {
            writeline(&quot;Source ended: err='&quot; + err + &quot;' &quot; + this);
            this.disconnect();
        };
        WebAudioSource.prototype.onBufferLoaded = function () {
            var _this = this;
            this._bufferSource.connect(this._destination);
            this._bufferSource.start(0);
            this._started = true;
            this._bufferSource.onended = function () {
                _this.handleEnd();
            };
        };
        WebAudioSource.prototype.toString = function () {
            return this._url;
        };
        return WebAudioSource;
    })();
    Bing.WebAudioSource = WebAudioSource;    
    var ArrayBufferSource = (function () {
        function ArrayBufferSource(context) {
            this.numberOfInputs = 1;
            this.numberOfOutputs = 1;
            this.channelCount = 1;
            if (!context) {
                if (!AudioContext) {
                    throw &quot;Sorry, your browser doesn't support WebAudio&quot;;
                }
                this.context = new AudioContext();
            } else {
                this.context = context;
            }
        }
        ArrayBufferSource.prototype.removeEventListener = function (type, listener, useCapture) {
        };
        ArrayBufferSource.prototype.addEventListener = function (type, listener, useCapture) {
        };
        ArrayBufferSource.prototype.dispatchEvent = function (evt) {
            return false;
        };
        ArrayBufferSource.prototype.connect = function (destination, output, input) {
            this._destination = destination;
            if (null != this._aBuffer &amp;&amp; this._aBuffer.byteLength &gt; 0) {
                this.bufferReceived();
                return;
            }
            writeline(&quot;connect &quot; + this);
        };
        ArrayBufferSource.prototype.disconnect = function () {
            var dest = this._destination;
            if (null == dest) {
                return;
            }
            if (this._bufferSource) {
                this._bufferSource.disconnect();
                this._bufferSource.stop();
            }
            this._started = false;
            dest.disconnect();
            this._destination = null;
            Platform.getCU().done(function (cu) {
                cu.disconnect();
            });
            if (this.onended) {
                this.onended();
            }
            dispatchAudioStop();
        };
        ArrayBufferSource.prototype.start = function (when, offset, duration) {
        };
        ArrayBufferSource.prototype.stop = function (when) {
        };
        ArrayBufferSource.prototype.setBuffer = function (buffer) {
            this._aBuffer = buffer;
        };
        ArrayBufferSource.prototype.bufferReceived = function () {
            var _this = this;
            var bufferSource;
            bufferSource = this.context.createBufferSource();
            decodeAudioData(this.context, this._aBuffer, function (buffer) {
                _this._bufferSource = bufferSource;
                _this._bufferSource.buffer = buffer;
                _this.onBufferLoaded();
            }, function () {
                writeline(&quot;error decoding WebAudio&quot;);
                _this.handleEnd();
            });
        };
        ArrayBufferSource.prototype.handleEnd = function (err) {
            writeline(&quot;Source ended: err='&quot; + err + &quot;' &quot; + this);
            this.disconnect();
        };
        ArrayBufferSource.prototype.onBufferLoaded = function () {
            var _this = this;
            this._bufferSource.connect(this._destination);
            this._bufferSource.start(0);
            this._started = true;
            this._bufferSource.onended = function () {
                _this.handleEnd();
            };
        };
        return ArrayBufferSource;
    })();
    Bing.ArrayBufferSource = ArrayBufferSource;    
    function SpeechMain() {
        var mediaNav = navigator;
        var acWindow = window;
        mediaNav.getUserMedia = mediaNav.getUserMedia || mediaNav.mozGetUserMedia || mediaNav.webkitGetUserMedia || mediaNav.msGetUserMedia || CreateActiveXObject(&quot;Bing.GetUserMedia&quot;);
        acWindow.AudioContext = acWindow.AudioContext || acWindow.webkitAudioContext || CreateActiveXObject(&quot;Bing.AudioContext&quot;);
        Bing._window.SpeechSynthesisUtterance = Bing._window.SpeechSynthesisUtterance || msSpeechSynthesisUtterance;
        Bing._window.SpeechRecognition = Bing.Speech;
        Bing._window.msSpeechSynthesis = new Bing.Synthesis();
        Bing._window.speechSynthesis = Bing._window.msSpeechSynthesis;
        Bing.initialize();
    }
    SpeechMain();
})(Bing || (Bing = {}));
</pre>
</body>
</html>
